home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 317 / asmsrc / atof-m68.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  13.8 KB  |  476 lines

  1. /* atof_m68k.c - */
  2.  
  3. /* Copyright (C) 1987 Free Software Foundation, Inc.
  4.  
  5. This file is part of Gas, the GNU Assembler.
  6.  
  7. The GNU assembler is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU Assembler General
  12. Public License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. the GNU Assembler, but only under the conditions described in the
  16. GNU Assembler General Public License.  A copy of this license is
  17. supposed to have been given to you along with the GNU Assembler
  18. so you can know your rights and responsibilities.  It should be
  19. in a file named COPYING.  Among other things, the copyright
  20. notice and this notice must be preserved on all copies.  */
  21.  
  22. #include "flonum.h"
  23.  
  24. extern FLONUM_TYPE generic_floating_point_number; /* Flonums returned here. */
  25. #define NULL (0)
  26.  
  27. extern char EXP_CHARS[];
  28.                 /* Precision in LittleNums. */
  29. #define MAX_PRECISION (6)
  30. #define F_PRECISION (2)
  31. #define D_PRECISION (4)
  32. #define X_PRECISION (6)
  33. #define P_PRECISION (6)
  34.  
  35.                 /* Length in LittleNums of guard bits. */
  36. #define GUARD (2)
  37.  
  38. int                /* Number of chars in flonum type 'letter'. */
  39. atof_sizeof (letter)
  40.      char letter;
  41. {
  42.   int    return_value;
  43.  
  44.   /*
  45.    * Permitting uppercase letters is probably a bad idea.
  46.    * Please use only lower-cased letters in case the upper-cased
  47.    * ones become unsupported!
  48.    */
  49.   switch (letter)
  50.     {
  51.     case 'f':
  52.     case 'F':
  53.     case 's':
  54.     case 'S':
  55.       return_value = F_PRECISION;
  56.       break;
  57.  
  58.     case 'd':
  59.     case 'D':
  60.     case 'r':
  61.     case 'R':
  62.       return_value = D_PRECISION;
  63.       break;
  64.  
  65.     case 'x':
  66.     case 'X':
  67.       return_value = X_PRECISION;
  68.       break;
  69.  
  70.     case 'p':
  71.     case 'P':
  72.       return_value = P_PRECISION;
  73.       break;
  74.  
  75.     default:
  76.       return_value = 0;
  77.       break;
  78.     }
  79.   return (return_value);
  80. }
  81.  
  82. static unsigned long int mask [] = {
  83.   0x00000000,
  84.   0x00000001,
  85.   0x00000003,
  86.   0x00000007,
  87.   0x0000000f,
  88.   0x0000001f,
  89.   0x0000003f,
  90.   0x0000007f,
  91.   0x000000ff,
  92.   0x000001ff,
  93.   0x000003ff,
  94.   0x000007ff,
  95.   0x00000fff,
  96.   0x00001fff,
  97.   0x00003fff,
  98.   0x00007fff,
  99.   0x0000ffff,
  100.   0x0001ffff,
  101.   0x0003ffff,
  102.   0x0007ffff,
  103.   0x000fffff,
  104.   0x001fffff,
  105.   0x003fffff,
  106.   0x007fffff,
  107.   0x00ffffff,
  108.   0x01ffffff,
  109.   0x03ffffff,
  110.   0x07ffffff,
  111.   0x0fffffff,
  112.   0x1fffffff,
  113.   0x3fffffff,
  114.   0x7fffffff,
  115.   0xffffffff
  116.   };
  117.  
  118. static int
  119. next_bits (number_of_bits, address_of_bits_left_in_littlenum, address_of_littlenum_pointer)
  120.      int        number_of_bits;
  121.      int *        address_of_bits_left_in_littlenum;
  122.      LITTLENUM_TYPE **    address_of_littlenum_pointer;
  123. {
  124.   int            return_value;
  125.  
  126.   if (number_of_bits >= (* address_of_bits_left_in_littlenum))
  127.     {
  128.       return_value  = mask [(* address_of_bits_left_in_littlenum)] & * (* address_of_littlenum_pointer);
  129.       number_of_bits -= (* address_of_bits_left_in_littlenum);
  130.       return_value <<= number_of_bits;
  131.       (* address_of_bits_left_in_littlenum) = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
  132.       (* address_of_littlenum_pointer) --;    /* JF was --, how could it
  133.                            possibly work! */
  134.       return_value |= ( (* (* address_of_littlenum_pointer)) >>
  135.                       ((* address_of_bits_left_in_littlenum)) ) & mask [number_of_bits];
  136.     }
  137.   else
  138.     {
  139.       (* address_of_bits_left_in_littlenum) -= number_of_bits;
  140.       return_value = mask [number_of_bits] & ( (* (* address_of_littlenum_pointer)) >> (* address_of_bits_left_in_littlenum));
  141.     }
  142.   return (return_value);
  143. }
  144.  
  145. static void
  146. make_invalid_floating_point_number (words)
  147.      LITTLENUM_TYPE *    words;
  148. {
  149.     words[0]= ((unsigned)-1)>>1;    /* Zero the leftmost bit */
  150.     words[1]= -1;
  151.     words[2]= -1;
  152.     words[3]= -1;
  153.     words[4]= -1;
  154.     words[5]= -1;
  155. }
  156.  
  157. /***********************************************************************\
  158. *                                    *
  159. *    Warning: this returns 16-bit LITTLENUMs, because that is    *
  160. *    what the VAX thinks in. It is up to the caller to figure    *
  161. *    out any alignment problems and to conspire for the bytes/word    *
  162. *    to be emitted in the right order. Bigendians beware!        *
  163. *                                    *
  164. \***********************************************************************/
  165.  
  166. char *                /* Return pointer past text consumed. */
  167. atof_m68k (str, what_kind, words)
  168.      char *        str;    /* Text to convert to binary. */
  169.      char        what_kind; /* 'd', 'f', 'g', 'h' */
  170.      LITTLENUM_TYPE *    words;    /* Build the binary here. */
  171. {
  172.     FLONUM_TYPE    f;
  173.     LITTLENUM_TYPE    bits [MAX_PRECISION + MAX_PRECISION + GUARD];
  174.                 /* Extra bits for zeroed low-order bits. */
  175.                 /* The 1st MAX_PRECISION are zeroed, */
  176.                 /* the last contain flonum bits. */
  177.     char *        return_value;
  178.     int        precision; /* Number of 16-bit words in the format. */
  179.     long int    exponent_bits;
  180.  
  181.     long int    exponent_1;
  182.     long int    exponent_2;
  183.     long int    exponent_3;
  184.     long int    exponent_4;
  185.     int        exponent_skippage;
  186.     LITTLENUM_TYPE    word1;
  187.     int            bits_left_in_littlenum;
  188.     LITTLENUM_TYPE *    littlenum_pointer;
  189.     LITTLENUM_TYPE *    lp;
  190.  
  191.     return_value = str;
  192.     f.low    = bits + MAX_PRECISION;
  193.     f.high    = NULL;
  194.     f.leader    = NULL;
  195.     f.exponent    = NULL;
  196.     f.sign    = '\0';
  197.  
  198.                 /* Use more LittleNums than seems */
  199.                 /* necessary: the highest flonum may have */
  200.                 /* 15 leading 0 bits, so could be useless. */
  201.  
  202.     bzero (bits, sizeof(LITTLENUM_TYPE) * MAX_PRECISION);
  203.  
  204.     switch(what_kind) {
  205.     case 'f':
  206.     case 'F':
  207.     case 's':
  208.     case 'S':
  209.         precision = F_PRECISION;
  210.         exponent_bits = 8;
  211.         break;
  212.  
  213.     case 'd':
  214.     case 'D':
  215.     case 'r':
  216.     case 'R':
  217.         precision = D_PRECISION;
  218. #ifdef atarist
  219.         exponent_bits = 8;    /* !!! fix this! */
  220. #else
  221.         exponent_bits = 11;
  222. #endif
  223.         break;
  224.  
  225.     case 'x':
  226.     case 'X':
  227.         precision = X_PRECISION;
  228.         exponent_bits = 15;
  229.         break;
  230.  
  231.     case 'p':
  232.     case 'P':
  233.         
  234.         precision = P_PRECISION;
  235.         exponent_bits= -1;
  236.         break;
  237.  
  238.     default:
  239.         make_invalid_floating_point_number (words);
  240.         return NULL;
  241.     }
  242.  
  243.     f.high = f.low + precision - 1 + GUARD;
  244.  
  245.     if (atof_generic (& return_value, ".", EXP_CHARS, & f)) {
  246.         as_warn("Error converting floating point number (Exponent overflow?)");
  247.         make_invalid_floating_point_number (words);
  248.         return NULL;
  249.     }
  250.  
  251.     if (f.low > f.leader) {
  252.         /* 0.0e0 seen. */
  253.         bzero (words, sizeof(LITTLENUM_TYPE) * precision);
  254.         return return_value;
  255.     }
  256.  
  257.         /*
  258.          * All vaxen floating_point formats (so far) have:
  259.          * Bit 15 is sign bit.
  260.          * Bits 14:n are excess-whatever exponent.
  261.          * Bits n-1:0 (if any) are most significant bits of fraction.
  262.          * Bits 15:0 of the next word are the next most significant bits.
  263.          * And so on for each other word.
  264.          *
  265.          * So we need: number of bits of exponent, number of bits of
  266.          * mantissa.
  267.          */
  268.     bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  269.     littlenum_pointer = f.leader;
  270.     /* Seek (and forget) 1st significant bit */
  271.     for (exponent_skippage = 0;! next_bits(1, &bits_left_in_littlenum,
  272.  &littlenum_pointer); exponent_skippage ++)
  273.         ;
  274.     exponent_1 = f.exponent + f.leader + 1 - f.low;
  275.     /* Radix LITTLENUM_RADIX, point just higher than f.leader. */
  276.     exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  277.     /* Radix 2. */
  278.     exponent_3 = exponent_2 - exponent_skippage;
  279.     /* Forget leading zeros, forget 1st bit. */
  280.     exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
  281.     /* Offset exponent. */
  282.  
  283.     if (exponent_4 & ~ mask [exponent_bits]) {
  284.             /*
  285.              * Exponent overflow. Lose immediately.
  286.              */
  287.  
  288.             /*
  289.              * We leave return_value alone: admit we read the
  290.              * number, but return a floating exception
  291.              * because we can't encode the number.
  292.              */
  293.  
  294.         as_warn("Exponent overflow in floating-point number");
  295.         make_invalid_floating_point_number (words);
  296.         return return_value;
  297.     }
  298.     lp = words;
  299.  
  300.     /* Word 1. Sign, exponent and perhaps high bits. */
  301.     /* Assume 2's complement integers. */
  302.     word1 = ((exponent_4 & mask [exponent_bits]) << (15 - exponent_bits)) |
  303.  ((f.sign == '+') ? 0 : 0x8000) | next_bits (15 - exponent_bits,
  304.  &bits_left_in_littlenum, &littlenum_pointer);
  305.     * lp ++ = word1;
  306.  
  307.     /* The rest of the words are just mantissa bits. */
  308.     for (; lp < words + precision; lp++)
  309.         * lp = next_bits (LITTLENUM_NUMBER_OF_BITS,
  310.  &bits_left_in_littlenum, &littlenum_pointer);
  311.  
  312.     if (next_bits (1, &bits_left_in_littlenum, &littlenum_pointer)) {
  313.         unsigned long int    carry;
  314.             /*
  315.              * Since the NEXT bit is a 1, round UP the mantissa.
  316.              * The cunning design of these hidden-1 floats permits
  317.              * us to let the mantissa overflow into the exponent, and
  318.              * it 'does the right thing'. However, we lose if the
  319.              * highest-order bit of the lowest-order word flips.
  320.              * Is that clear?
  321.              */
  322.  
  323.  
  324. /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  325.     Please allow at least 1 more bit in carry than is in a LITTLENUM.
  326.     We need that extra bit to hold a carry during a LITTLENUM carry
  327.     propagation. Another extra bit (kept 0) will assure us that we
  328.     don't get a sticky sign bit after shifting right, and that
  329.     permits us to propagate the carry without any masking of bits.
  330. #endif */
  331.         for (carry = 1, lp --; carry && (lp >= words); lp --) {
  332.             carry = * lp + carry;
  333.             * lp = carry;
  334.             carry >>= LITTLENUM_NUMBER_OF_BITS;
  335.         }
  336.         if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) ) {
  337.             /* We leave return_value alone: admit we read the
  338.              * number, but return a floating exception
  339.              * because we can't encode the number.
  340.              */
  341.             make_invalid_floating_point_number (words);
  342.             return return_value;
  343.         }
  344.     }
  345.     return (return_value);
  346. }
  347.  
  348. gen_to_words(words,precision,exponent_bits)
  349. LITTLENUM_TYPE *words;
  350. long int    exponent_bits;
  351. {
  352.     int return_value=0;
  353.  
  354.     long int    exponent_1;
  355.     long int    exponent_2;
  356.     long int    exponent_3;
  357.     long int    exponent_4;
  358.     int        exponent_skippage;
  359.     LITTLENUM_TYPE    word1;
  360.     int            bits_left_in_littlenum;
  361.     LITTLENUM_TYPE *    littlenum_pointer;
  362.     LITTLENUM_TYPE *    lp;
  363.  
  364.     if (generic_floating_point_number.low > generic_floating_point_number.leader) {
  365.         /* 0.0e0 seen. */
  366.         bzero (words, sizeof(LITTLENUM_TYPE) * precision);
  367.         return return_value;
  368.     }
  369.  
  370.         /*
  371.          * All vaxen floating_point formats (so far) have:
  372.          * Bit 15 is sign bit.
  373.          * Bits 14:n are excess-whatever exponent.
  374.          * Bits n-1:0 (if any) are most significant bits of fraction.
  375.          * Bits 15:0 of the next word are the next most significant bits.
  376.          * And so on for each other word.
  377.          *
  378.          * So we need: number of bits of exponent, number of bits of
  379.          * mantissa.
  380.          */
  381.     bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  382.     littlenum_pointer = generic_floating_point_number.leader;
  383.     /* Seek (and forget) 1st significant bit */
  384.     for (exponent_skippage = 0;! next_bits(1, &bits_left_in_littlenum,
  385.  &littlenum_pointer); exponent_skippage ++)
  386.         ;
  387.     exponent_1 = generic_floating_point_number.exponent + generic_floating_point_number.leader + 1 -
  388.  generic_floating_point_number.low;
  389.     /* Radix LITTLENUM_RADIX, point just higher than generic_floating_point_number.leader. */
  390.     exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  391.     /* Radix 2. */
  392.     exponent_3 = exponent_2 - exponent_skippage;
  393.     /* Forget leading zeros, forget 1st bit. */
  394.     exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
  395.     /* Offset exponent. */
  396.  
  397.     if (exponent_4 & ~ mask [exponent_bits]) {
  398.             /*
  399.              * Exponent overflow. Lose immediately.
  400.              */
  401.  
  402.             /*
  403.              * We leave return_value alone: admit we read the
  404.              * number, but return a floating exception
  405.              * because we can't encode the number.
  406.              */
  407.  
  408.         make_invalid_floating_point_number (words);
  409.         return return_value;
  410.     }
  411.     lp = words;
  412.  
  413.     /* Word 1. Sign, exponent and perhaps high bits. */
  414.     /* Assume 2's complement integers. */
  415.     word1 = ((exponent_4 & mask [exponent_bits]) << (15 - exponent_bits)) |
  416.  ((generic_floating_point_number.sign == '+') ? 0 : 0x8000) | next_bits (15 - exponent_bits,
  417.  &bits_left_in_littlenum, &littlenum_pointer);
  418.     * lp ++ = word1;
  419.  
  420.     /* The rest of the words are just mantissa bits. */
  421.     for (; lp < words + precision; lp++)
  422.         * lp = next_bits (LITTLENUM_NUMBER_OF_BITS,
  423.  &bits_left_in_littlenum, &littlenum_pointer);
  424.  
  425.     if (next_bits (1, &bits_left_in_littlenum, &littlenum_pointer)) {
  426.         unsigned long int    carry;
  427.             /*
  428.              * Since the NEXT bit is a 1, round UP the mantissa.
  429.              * The cunning design of these hidden-1 floats permits
  430.              * us to let the mantissa overflow into the exponent, and
  431.              * it 'does the right thing'. However, we lose if the
  432.              * highest-order bit of the lowest-order word flips.
  433.              * Is that clear?
  434.              */
  435.  
  436.  
  437. /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  438.     Please allow at least 1 more bit in carry than is in a LITTLENUM.
  439.     We need that extra bit to hold a carry during a LITTLENUM carry
  440.     propagation. Another extra bit (kept 0) will assure us that we
  441.     don't get a sticky sign bit after shifting right, and that
  442.     permits us to propagate the carry without any masking of bits.
  443. #endif */
  444.         for (carry = 1, lp --; carry && (lp >= words); lp --) {
  445.             carry = * lp + carry;
  446.             * lp = carry;
  447.             carry >>= LITTLENUM_NUMBER_OF_BITS;
  448.         }
  449.         if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) ) {
  450.             /* We leave return_value alone: admit we read the
  451.              * number, but return a floating exception
  452.              * because we can't encode the number.
  453.              */
  454.             make_invalid_floating_point_number (words);
  455.             return return_value;
  456.         }
  457.     }
  458.     return (return_value);
  459. }
  460.  
  461. /* This routine is a real kludge.  Someone really should do it better, but
  462.    I'm too lazy, and I don't understand this stuff all too well anyway
  463.    (JF)
  464.  */
  465. int_to_gen(x)
  466. long x;
  467. {
  468.     char buf[20];
  469.     char *bufp;
  470.  
  471.     sprintf(buf,"%ld",x);
  472.     bufp= &buf[0];
  473.     if(atof_generic(&bufp,".", EXP_CHARS, &generic_floating_point_number))
  474.         as_warn("Error converting number to floating point (Exponent overflow?)");
  475. }
  476.